package com.baizhi.controller;

import com.baizhi.entity.PmsAttr;
import com.baizhi.entity.PmsAttrAttrgroupRelation;
import com.baizhi.entity.PmsAttrGroup;
import com.baizhi.entity.PmsCategory;
import com.baizhi.service.IPmsAttrAttrgroupRelationService;
import com.baizhi.service.IPmsAttrGroupService;
import com.baizhi.service.IPmsAttrService;
import com.baizhi.service.PmsCategoryService;
import com.baizhi.vo.PmsAttrAddGroupVo;
import com.baizhi.vo.PmsAttrInsertVo;
import com.baizhi.vo.PmsAttrVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author yu
 */
@RestController
public class PmsAttrController {
    @Autowired
    private IPmsAttrService iPmsAttrService;
    @Autowired
    private PmsCategoryService pmsCategoryService;
    @Autowired
    private IPmsAttrGroupService pmsAttrGroupService;
    @Autowired
    private IPmsAttrAttrgroupRelationService iPmsAttrAttrgroupRelationService;


    @RequestMapping("/pmsAttrs/showAllByCategoryId")
    public Map showAll(Integer page,Integer limit,Integer type,Integer categoryId){
        HashMap<String, Object> map = new HashMap<>();
        Page<PmsAttr> page1 = new Page<>(page, limit);
        QueryWrapper<PmsAttr> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_type",type).eq("category_id",categoryId);
        Page<PmsAttr> page2 = iPmsAttrService.page(page1, wrapper);
        long count = page2.getTotal();
        List<PmsAttr> list = page2.getRecords();
        List<PmsAttrVo> collect = list.stream().map(new Function<PmsAttr, PmsAttrVo>() {
            @Override
            public PmsAttrVo apply(PmsAttr pmsAttr) {
                PmsAttrVo pmsAttrVo = new PmsAttrVo();
                BeanUtils.copyProperties(pmsAttr, pmsAttrVo);
                PmsCategory category = pmsCategoryService.getById(pmsAttr.getCategoryId());
                pmsAttrVo.setCategoryName(category.getName());
                QueryWrapper<PmsAttrAttrgroupRelation> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("attr_id", pmsAttr.getAttrId());
                PmsAttrAttrgroupRelation pmsAttrAttrgroupRelation = iPmsAttrAttrgroupRelationService.getOne(queryWrapper);
                if (pmsAttrAttrgroupRelation!=null){
                    Long attrGroupId = pmsAttrAttrgroupRelation.getAttrGroupId();
                    pmsAttrVo.setAttrGroupName(pmsAttrGroupService.getById(attrGroupId).getAttrGroupName());
                }
                return pmsAttrVo;
            }
        }).collect(Collectors.toList());
        map.put("code",0);
        map.put("count",count);
        map.put("data",collect);
        return map;
    }

    @RequestMapping("/pmsAttrs/add")
    public Map insert(PmsAttrInsertVo pmsAttrInsertVo){
        HashMap<String, Object> map = new HashMap<>();
        String categoryName = pmsAttrInsertVo.getCategoryName();
        QueryWrapper<PmsCategory> wrapper = new QueryWrapper<>();
        PmsAttr pmsAttr = new PmsAttr();
        if (categoryName!=null){
            wrapper.eq("name",pmsAttrInsertVo.getCategoryName());
            PmsCategory pmsCategory = pmsCategoryService.getOne(wrapper);
            pmsAttr.setCategoryId(pmsCategory.getCatId());
        }
        BeanUtils.copyProperties(pmsAttrInsertVo,pmsAttr);
        iPmsAttrService.save(pmsAttr);
        if (pmsAttrInsertVo.getAttrGroupId()!=null){
            PmsAttrAttrgroupRelation pmsAttrAttrgroupRelation = new PmsAttrAttrgroupRelation();
            pmsAttrAttrgroupRelation.setAttrId(pmsAttr.getAttrId());
            pmsAttrAttrgroupRelation.setAttrGroupId(pmsAttrInsertVo.getAttrGroupId());
            iPmsAttrAttrgroupRelationService.save(pmsAttrAttrgroupRelation);
        }
        map.put("status","success");
        return map;
    }

    @RequestMapping("/pmsAttr/showNoTeamAttr")
    public Map showNoTeamAttrByCategoryId(Integer categoryId){
        HashMap<String, Object> map = new HashMap<>();
        QueryWrapper<PmsAttr> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("category_id",categoryId).eq("attr_type",0);
        List<PmsAttr> list = iPmsAttrService.list(wrapper1);
        QueryWrapper<PmsAttrGroup> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("category_id",categoryId);
        List<PmsAttrGroup> list1 = pmsAttrGroupService.list(wrapper2);
        List<Long> longs = new ArrayList<>();
        for (PmsAttrGroup pmsAttrGroup : list1) {
            longs.add(pmsAttrGroup.getAttrGroupId());
        }
        QueryWrapper<PmsAttrAttrgroupRelation> wrapper3 = new QueryWrapper<>();
        wrapper3.in("attr_group_id",longs);
        List<PmsAttrAttrgroupRelation> list2 = iPmsAttrAttrgroupRelationService.list(wrapper3);
        ArrayList<Long> longs1 = new ArrayList<>();
        for (PmsAttrAttrgroupRelation pmsAttrAttrgroupRelation : list2) {
            longs1.add(pmsAttrAttrgroupRelation.getAttrId());
        }
        List<PmsAttr> collect = list.stream().filter(new Predicate<PmsAttr>() {
            @Override
            public boolean test(PmsAttr pmsAttr) {
                for (Long aLong : longs1) {
                    if (aLong == pmsAttr.getAttrId()) {
                        return false;
                    }
                }
                return true;
            }
        }).collect(Collectors.toList());
        map.put("data",collect);
        return map;
    }

   @DeleteMapping("/pmsAttrs/{id}")
   public Map delete(Integer id) {
       iPmsAttrService.removeById(id);
       PmsAttr pmsAttr = iPmsAttrService.getById(id);
       QueryWrapper<PmsAttrAttrgroupRelation> wrapper = new QueryWrapper<>();
       wrapper.eq("attr_id", id);
       iPmsAttrAttrgroupRelationService.remove(wrapper);
       HashMap<String, Object> map = new HashMap<>();
       map.put("status", "success");
       return map;
   }

   @PostMapping("/pmsAttrs")
    public Map addLink(@RequestBody PmsAttrAddGroupVo pmsAttrAddGroupVo){
        /*需要添加个VO 接受类别名和未分组的属性 在表中建立关联*/
        HashMap<String, Object> map = new HashMap<>();
        PmsAttrAttrgroupRelation relation = new PmsAttrAttrgroupRelation();
        relation.setAttrId(pmsAttrAddGroupVo.getAttrId());
        relation.setAttrGroupId(pmsAttrAddGroupVo.getAttrGroupId());
        iPmsAttrAttrgroupRelationService.save(relation);
        map.put("status","success");
        return map;
    }



}
